package com.gxa.learning.pc.service.impl;

import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Cost;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.pc.mapper.CostMapper;
import com.gxa.learning.pc.service.CostService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @version E-learning
 * @Author lonny
 * @Date 2021/12/7 10:06
 */


@Service
@Transactional(rollbackFor = Exception.class)
public class CostServiceImpl extends BaseServiceImpl<Cost> implements CostService {

    @Autowired
    private CostMapper costMapper;


    /**
     * 通过成本名和成本类型
     * 查询成本
     *
     * @param cost 对象
     * @return 返回
     * @throws Exception 抛出
     */
    @Override
    public Cost findByCost(Cost cost) throws Exception {
        if (cost == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_NOT_NULL);
        }
        return costMapper.findByCost(cost);
    }


    @Override
    public void insertCost(Cost cost) throws Exception {
        Cost byCost = findByCost(cost);
        if (byCost != null) {
            throw new BusinessException(ExceptionCode.REPEAT_COST_WRONG);
        }
        int insert = costMapper.insert(cost);
        if (insert != 1) {
            throw new BusinessException(ExceptionCode.INSERT_COST_WRONG);
        }
    }


    /**
     * 更新
     *
     * @param cost 对象
     * @throws Exception 抛出
     */
    @Override
    public void updateCost(Cost cost) throws Exception {
        if (StringUtils.isEmpty(cost.getName()) || StringUtils.isBlank(cost.getName())) {
            throw new BusinessException(ExceptionCode.COST_NAME_NULL);
        }

        System.out.println(cost);
        int i = costMapper.updateCost(cost);
        if (i != 1) {
            throw new BusinessException(ExceptionCode.WRONG_UPLOAD);
        }
    }


    @Override
    public void deleteCost(String id) throws Exception {
        int i = costMapper.deleteByPrimaryKey(id);
        if (i == 0) {
            throw new BusinessException(ExceptionCode.WRONG_DELETE);
        }
    }
}
